package com.example.knowman.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.OBKnowCategoryService;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ObService.OBlabelCategoryService;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.dao.knowLabelMainEx;
import com.example.knowman.entity.archLabelMain;
import com.example.knowman.entity.knowLabelMain;
import com.example.knowman.entity.labelCategory;
import com.example.knowman.mapper.ArchLabelMainMapper;
import com.example.knowman.mapper.KnowLabelMainMapper;
import com.example.knowman.service.IArchLabelMainService;
import com.example.knowman.service.IknowLabelMainService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArchLabelMainServiceImpl extends ServiceImpl<ArchLabelMainMapper, archLabelMain> implements IArchLabelMainService {

    private final OBKnowMainService obKnowMainService;

    private final OBlabelCategoryService oblabelCategoryService;

    private final OBKnowCategoryService obKnowCategoryService;

    public ArchLabelMainServiceImpl(OBKnowMainService obKnowMainService, OBlabelCategoryService oblabelCategoryService, OBKnowCategoryService obKnowCategoryService) {
        this.obKnowMainService = obKnowMainService;
        this.oblabelCategoryService = oblabelCategoryService;
        this.obKnowCategoryService = obKnowCategoryService;
    }
    @Override
    public Message updateByknowLblId(Integer archLblId, String lblContent, String notes,String userName) {
        Message message = new Message();
        this.lambdaUpdate().eq(archLabelMain::getArchLblId,archLblId)
                .set(lblContent != null && !lblContent.isEmpty(),archLabelMain::getLblContent,lblContent)
                .set(notes != null && !notes.isEmpty(),archLabelMain::getNotes,notes)
                .set(userName != null && !userName.isEmpty(),archLabelMain::getUserName,userName)
                .update();

        message.setCode(200);
        message.setTip("修改成功");

        return message;
    }

    @Override
    public archLabelMain getSingle(Integer knowId) {
        return this.lambdaQuery().select().eq(archLabelMain::getKnowId,knowId).one();
    }
//
    @Override
    public Message Insert(Integer knowId, String lblContent, String userName, String notes) {
        Message message = new Message();

        archLabelMain temp = new archLabelMain();
        temp.setKnowId(knowId);
        temp.setLblContent(lblContent);
        temp.setUserName(userName);
        temp.setNotes(notes);

        try{
            this.save(temp);
            message.setCode(200);
            message.setTip("新增成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("新增失败");
        }

        return message;
    }
//
//    @Override
//    public knowLabelMainEx getLabelsByKnowId(Integer knowId) {
//        Integer knowCateId = obKnowMainService.returnknowCateIdByknowId(knowId);
//        String knowCateName = obKnowCategoryService.returnKnowCateNameByKnowCateId(knowCateId);
//        List<labelCategory> lis = oblabelCategoryService.getLabelsByKnowCateId(knowCateId);
//
//        knowLabelMainEx res = new knowLabelMainEx();
//        res.setKnowCateId(knowCateId);
//        res.setKnowCateName(knowCateName);
//        res.setList(lis);
//        return res;
//    }

    @Override
    public List<archLabelMain> getAll() {
        return this.lambdaQuery().list();
    }

    @Override
    public knowLabelMainEx getLabelsByknowCateId(Integer knowCateId) {
        String knowCateName = obKnowCategoryService.returnKnowCateNameByKnowCateId(knowCateId);
        List<labelCategory> lis = oblabelCategoryService.getLabelsByKnowCateId(knowCateId);

        knowLabelMainEx res = new knowLabelMainEx();
        res.setKnowCateId(knowCateId);
        res.setKnowCateName(knowCateName);
        res.setList(lis);
        return res;
    }

    @Override
    public archLabelMain getSingleByArchLblId(Integer archLblId) {
        return this.lambdaQuery().select().eq(archLabelMain::getArchLblId,archLblId).one();
    }


}
